#ifndef HFT_SMART_ROUTER_H
#define HFT_SMART_ROUTER_H

#include "execution/order_manager.h"
#include <memory>
#include <string>
#include <vector>
#include <unordered_map>
#include <mutex>
#include <spdlog/spdlog.h>

namespace hft {

struct OrderBook {
    std::string symbol;
    int64_t timestamp;
    std::vector<std::pair<double, int64_t>> bids;
    std::vector<std::pair<double, int64_t>> asks;
};

class OrderExecutor {
public:
    virtual ~OrderExecutor() = default;
    virtual bool submitOrder(const Order& order) = 0;
};

class SmartOrderRouterImpl {
public:
    explicit SmartOrderRouterImpl(std::shared_ptr<OrderExecutor> executor);
    
    bool routeOrder(const Order& order);
    bool batchRouteOrders(const std::vector<Order>& orders);
    
    void onOrderBookUpdate(const OrderBook& book);
    void onMarketData(const MarketDataMessage& msg);
    void onOrderUpdate(const Order& order);
    
    void setMaxSlippage(double max_slippage);
    void setMinLiquidity(int64_t min_liquidity);

    struct LiquidityCheckResult {
        int64_t available = 0;
        bool sufficient = false;
    };
    
    struct SlippageCheckResult {
        double actual = 0.0;
        bool acceptable = false;
    };

private:
    std::shared_ptr<OrderExecutor> executor_;
    double max_slippage_;
    int64_t min_liquidity_;
    std::unordered_map<std::string, OrderBook> order_books_;
    std::mutex books_mutex_;
    
    LiquidityCheckResult checkLiquidity(const Order& order, const OrderBook& book);
    SlippageCheckResult checkSlippage(const Order& order, const OrderBook& book);
};

} // namespace hft

#endif // HFT_SMART_ROUTER_H